package com.springboot.kaoqing.controller;


import cn.hutool.http.server.HttpServerRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.kaoqing.entity.Kecheng;
import com.springboot.kaoqing.entity.Qiandao;
import com.springboot.kaoqing.entity.SysUser;
import com.springboot.kaoqing.qpage.QPage;
import com.springboot.kaoqing.service.IKechengService;
import com.springboot.kaoqing.service.IQiandaoService;
import com.springboot.kaoqing.service.ISysUserService;
import com.springboot.kaoqing.util.QRCodeUtil;
import com.springboot.kaoqing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 签到记录表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-06
 */
@Controller
@RequestMapping("/api/qiandao")
public class QiandaoController {


    @Autowired
    private ISysUserService iUsersService;

    @Autowired
    private IKechengService iKechengService;

    @Autowired
    private IQiandaoService service;

    /**
     * 路由地址
     */
    public String prefix = "/view/admin/jiaoshi";


    /**
     * 进入管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/qiandao";
    }

    /**
     * 进入教师页面
     * @return
     */
    @RequestMapping("/xuesheng/index")
    public String xueshengindex(){
        return "/view/admin/xuesheng/qiandao";
    }


    /**
     * 获取所有的列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result getList(QPage qPage, Qiandao obj, HttpServletRequest request) {
        SysUser sysUser=(SysUser) request.getSession().getAttribute("user");
        if(sysUser.getType()==2){//教师查看自己学生签到记录

            obj.setUid(sysUser.getId());
        }else if(sysUser.getType()==3){//学生查看自己的
            obj.setSid(sysUser.getId());
        }
        return Result.reOk("查询成功",this.service.getPageList(qPage,obj));
    }

    /**
     * 签到
     * @return
     */
    @RequestMapping("/qiandao")
    @ResponseBody
    public Result qiandao(int id, String qrcodenum, HttpServletRequest request) {
        Kecheng kecheng=this.iKechengService.getById(id);
        if(qrcodenum==null||qrcodenum.equals("")){
            return Result.reError("请输入校验码");
        }
        if(!kecheng.getQrcodenum().equals(qrcodenum)){
            return Result.reError("签到校验失败");
        }
        //验证是否重复签到
        SysUser sysUser=(SysUser) request.getSession().getAttribute("user");
        QueryWrapper<Qiandao> qiandaoQueryWrapper=new QueryWrapper<>();
        qiandaoQueryWrapper.eq("sid",sysUser.getId());
        qiandaoQueryWrapper.eq("kid",kecheng.getId());
        List<Qiandao> qiandaoList=this.service.list(qiandaoQueryWrapper);
        if(qiandaoList!=null&&qiandaoList.size()>=1){
            return Result.reError("你已对课程签到，不要重复签到");
        }
        //插入数据
        Qiandao qiandao=new Qiandao();
        qiandao.setCtime(new Date());
        qiandao.setKid(kecheng.getId());
        qiandao.setKname(kecheng.getKname());
        qiandao.setKno(kecheng.getKno());
        qiandao.setSid(sysUser.getId());
        qiandao.setUid(kecheng.getUid());
        qiandao.setUname(kecheng.getUname());
        qiandao.setSname(sysUser.getNickname());
        boolean isOk=this.service.save(qiandao);
        if(isOk){
            return Result.reOk("签到成功");
        }
        return Result.reError("签到失败");
    }




}
